package com.lyfei.ydc.api;

import com.lyfei.ydc.utils.OssUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/common")
public class CommonController {
    @PostMapping("/upload")
    public Map<String, String> uploadFile(MultipartFile file) throws Exception {
        Map<String, String> map = new HashMap<>();
        try {
            List<String> result = OssUtils.upload(file);
            if(result.size()==2){
                map.put("url", result.get(1));
                map.put("fileName", result.get(0));
                return map;
            }else{
                throw new Exception("图片上传失败");
            }
        }catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
}
